// 全局注册Liquid渲染测试更新
var Liquid = window.liquidjs.Liquid;
window.engine = new Liquid({
    extname: ".html",
    cache: true,
    globals: {
        theme_config: window.themeConfig || {}
    }
});
window.engine.registerFilter("current_countrys", (item, key) => {
    if (!item) {
        return "https://imgcdn.oemsaas.cn/statics/country/flag/default.png";
    } else {
        const file = oemcart_countrys[item].country_code_2.toLowerCase();
        return `https://imgcdn.oemsaas.cn/statics/country/flag/${file}.png`;
    }
});

window.engine.registerFilter("money", (item, key) => {
    return formatMoney(parseFloat(item || 0), window.oemcart_currency);
});

window.engine.registerFilter("public_asset_abs_url", (item, key) => {
    return "/theme/default/assets/" + item;
});
window.engine.registerFilter("asset_abs_dir_url", (item, key) => {
    return "/theme/default/assets/" + item;
});
window.engine.registerFilter("public_asset_abs_dir_url", (item, key) => {
    return "/theme/default/assets/" + item;
});

//全局搜索方法
window.oemsaasSearch = function (classStr) {
    const themeId = moi.getUrlParam("theme_id");
    const searchValue = $(classStr).val();
    if (!searchValue) {
        return false;
    }
    const URL = location.href.indexOf("/search") > -1 ? location.href : "/search";
    location.href = moi.setUrlParams(URL, { q: searchValue, page: 1, theme_id: themeId });
    return false;
};

window.oemsaasMiniCart = function () {
    moi.onEvent("showMiniCart");
    $(".minicart").addClass("minicart_on");
    const html = $(".minicart").find(".minicart_mid").html();
    if (!html) {
        miniCart();
    }
};

window.addCartNum = function () {
    moi.ajax({
        url: "/cart.json",
        type: "GET",
        dataType: "json",
        success: function (res) {
            var num = res.data.item_number;
            moi.changeCartNumber(num);
        }
    });
};

window.miniCart = function () {
    $(".minicart_form").remove();
    $.ajax({
        url: "/cart/mini",
        cache: false,
        dataType: "html",
        success: function (html) {
            $(".minicart_mid").html(html);
            moi.onEvent("miniCart");
        },
        beforeSend: function () {
            $(".minicart_top").after(
                '<div class="minicart_ajaxloading"><svg t="1592222422836" class="order_btn_icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6126"><path d="M512.606821 958.898283c-246.319012 0-446.698738-200.379727-446.698738-446.698738S266.287809 65.479317 512.606821 65.479317c17.73185 0 32.117488 14.385639 32.117488 32.117488s-14.385639 32.117488-32.117488 32.117488c-210.897268 0-382.463762 171.58696-382.463762 382.484228s171.566494 382.463762 382.463762 382.463762 382.484228-171.566494 382.484228-382.463762c0-106.013499-42.384319-204.603935-119.332852-277.558503-12.859889-12.211113-13.403265-32.536021-1.212618-45.416376 12.190647-12.901845 32.536021-13.403265 45.416376-1.212618 89.870844 85.229127 139.365094 200.35926 139.365094 324.187497C959.327048 758.518556 758.925832 958.898283 512.606821 958.898283z" p-id="6127"></path></svg></div>'
            );
        },
        complete: function () {
            $(".minicart_ajaxloading").remove();
        }
    });
};

window.createAjaxLoading = function () {
    $("body").append(
        '<div class="ajaxloading"><svg t="1592222422836" class="order_btn_icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6126"><path d="M512.606821 958.898283c-246.319012 0-446.698738-200.379727-446.698738-446.698738S266.287809 65.479317 512.606821 65.479317c17.73185 0 32.117488 14.385639 32.117488 32.117488s-14.385639 32.117488-32.117488 32.117488c-210.897268 0-382.463762 171.58696-382.463762 382.484228s171.566494 382.463762 382.463762 382.463762 382.484228-171.566494 382.484228-382.463762c0-106.013499-42.384319-204.603935-119.332852-277.558503-12.859889-12.211113-13.403265-32.536021-1.212618-45.416376 12.190647-12.901845 32.536021-13.403265 45.416376-1.212618 89.870844 85.229127 139.365094 200.35926 139.365094 324.187497C959.327048 758.518556 758.925832 958.898283 512.606821 958.898283z" p-id="6127"></path></svg></div>'
    );
};

window.removeAjaxLoading = function () {
    $(".ajaxloading").remove();
};

//关闭快速预览
window.quickViewClose = function (callback) {
    if (moi.globalpopupLayer) {
        moi.globalpopupLayer.close(callback);
    }
};
//快速预览加入购物车
window.quickViewCart = function (params) {
    moi.addEvent(
        "quickViewAddCart",
        function () {
            oemsaasMiniCart();
        },
        true
    );
    quickViewClose(function () {
        var num = parseInt(moi.getPageStorage("cart_number", 0)) + params.quantity;
        moi.changeCartNumber(num);
        miniCart();
        moi.onEvent("quickViewAddCart", {
            product_id: params.product_id,
            collection_id: []
        });
    });
};
//快速预览立即购买
window.quickViewBuy = function (url) {
    quickViewClose(function () {
        moi.href(url);
    });
};
//查看详情
window.quickViewDetail = function (url) {
    quickViewClose(function () {
        moi.href(url);
    });
};

//显示快速预览
window.quickViewLoad = function (height) {
    if (!moi.globalpopupLayer) {
        return false;
    }

    const classStr = themeConfig.global.product_quickview_type == "normal" ? "normal-quick-iframe" : "simple-quick-iframe";
    const quickIframe = moi.globalpopupLayer.temp.find("#quick-iframe");
    const iframeWrapNode = moi.globalpopupLayer.temp.find(".quick-iframe-wrap");
    moi.globalpopupLayer.temp.find(".quick-iframe-wrap-loading").remove();
    quickIframe.height(height);
    quickIframe.addClass(classStr);

    setTimeout(function () {
        if (height > iframeWrapNode.innerHeight()) {
            console.log(iframeWrapNode, iframeWrapNode.innerHeight());
            quickIframe.attr("scrolling", "auto");
        }
    }, 500);
};
window.oemsaasQuickview = moi.throttle(function (e) {
    e.stopPropagation();
    e.preventDefault();
    moi.popupProductPicture({
        handle: $(this).data("handle"),
        data_from: $(this).data("from")
    });
}, 1000);
